package com.wcl.demo.model;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.wcl.demo.db.UserHelper;
import com.wcl.demo.model.modelinterface.ModelRegisterInterface;
import com.wcl.demo.presenter.presenterinterface.PresenterRegisterInterface;

import java.util.Map;

/**
 * Created by dllo on 15/11/20.
 */
public class ModelRegister implements ModelRegisterInterface {
    private UserHelper userHelper;

    public ModelRegister() {
        userHelper = new UserHelper();
    }

    @Override
    public void recvieveResult(Map<String, String> map, final PresenterRegisterInterface presenterRegisterInterface) {
        final String user_name = map.get("user_name");
        final String user_psw = map.get("user_psw");
        String user_confirm_psw = map.get("user_confirm_psw");
        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                presenterRegisterInterface.sendResult("注册成功");
                return false;
            }
        });


        if (TextUtils.isEmpty(user_name)) {
            return;
        } else if (TextUtils.isEmpty(user_psw)) {
            return;
        } else if (TextUtils.isEmpty(user_confirm_psw)) {
            return;
        }

        if (!TextUtils.isEmpty(user_name) && !TextUtils.isEmpty(user_psw)) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    try {
                        EMChatManager.getInstance().createAccountOnServer(user_name, user_psw);
                        handler.sendEmptyMessage(001);
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                        int errorCode = e.getErrorCode();
                        if (errorCode == EMError.NONETWORK_ERROR) {

                        } else if (errorCode == EMError.USER_ALREADY_EXISTS) {

                        } else if (errorCode == EMError.UNAUTHORIZED) {

                        } else {

                        }
                    }

                }
            }).start();

        }
    }
}
